package com.geek.factory.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.geek.factory.constant.SuccessConstant;
import com.geek.factory.dto.DeviceRepairReportDTO;
import com.geek.factory.entity.DeviceRepairReport;
import com.geek.factory.result.Result;
import com.geek.factory.service.DeviceRepairReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/device-repair-report")
public class DeviceRepairReportController {

    @Autowired
    private DeviceRepairReportService reportService;

    /**
     * 添加报修记录
     */
    @PostMapping("/add")
    public Result add(@RequestBody DeviceRepairReport report) {
        boolean success = reportService.save(report);
        return new Result(success ? SuccessConstant.SUCCESS : SuccessConstant.FAIL,
                success ? "上报成功" : "上报失败");
    }

    /**
     * 更新报修记录
     */
    @PutMapping("/update")
    public Result update(@RequestBody DeviceRepairReport report) {
        boolean success = reportService.updateById(report);
        return new Result(success ? SuccessConstant.SUCCESS : SuccessConstant.FAIL,
                success ? "更新成功" : "更新失败");
    }

    /**
     * 删除报修记录
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        boolean success = reportService.removeById(id);
        return new Result(success ? SuccessConstant.SUCCESS : SuccessConstant.FAIL,
                success ? "删除成功" : "删除失败");
    }

    /**
     * 分页查询设备维修报修单（带产线名称）
     */
    @GetMapping("/page")
    public Result getPagedReports(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size
    ) {
        IPage<DeviceRepairReportDTO> resultPage = reportService.getPagedReports(page, size);
        return new Result(SuccessConstant.SUCCESS, "查询成功", resultPage);
    }

    /**
     * 根据 ID 查询某一条报修单（带产线名称）
     */
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable Long id) {
        DeviceRepairReportDTO dto = reportService.getReportByIdWithLineName(id);
        if (dto != null) {
            return new Result(SuccessConstant.SUCCESS, "查询成功", dto);
        } else {
            return new Result(SuccessConstant.FAIL, "未找到该报修单");
        }
    }


}
